package com.hxf.service.base.impl;

import java.io.Serializable;
import java.util.List;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Repository;
  
import com.hxf.dao.facade.DAOFacade;
import com.hxf.dto.BaseDto;
import com.hxf.exception.DaoLevelException;
import com.hxf.exception.ServiceException;
import com.hxf.service.base.BaseService;

/**
 * service公共接口实现类
 * @author tengyicheng
 * @version [1.0, 2013/09/22]
 * @see [相关类/方法]
 * @since [产品/模块版本]
 */
@Repository("baseService")
public class BaseServiceImpl implements BaseService {
	protected transient final Logger logger = Logger.getLogger(getClass());

	@Resource
	protected DAOFacade daoFacade;
	
	@Override
	public void save(BaseDto entity) throws ServiceException {
		try {
			this.daoFacade.getBaseDao().create(entity);
		} catch (DaoLevelException e) {
			throw new ServiceException(e.getMessage(), e);
		}
	}

	@Override
	public void delete(BaseDto entity) throws ServiceException {
		try {
			this.daoFacade.getBaseDao().delete(entity);
		} catch (DaoLevelException e) {
			throw new ServiceException(e.getMessage(), e);
		}
	}

	@Override
	public BaseDto load(Class clazz, Serializable id) throws ServiceException {
		try {
			return this.daoFacade.getBaseDao().load(clazz, id);
		} catch (DaoLevelException e) {
			throw new ServiceException(e.getMessage(), e);
		}
	}

	@Override
	public void saveOrUpdate(BaseDto entity) throws ServiceException {
		try {
			this.daoFacade.getBaseDao().saveOrUpdate(entity);
		} catch (DaoLevelException e) {
			throw new ServiceException(e.getMessage(), e);
		}
	}

	@Override
	public void update(BaseDto entity) throws ServiceException {
		try {
			this.daoFacade.getBaseDao().update(entity);
		} catch (DaoLevelException e) {
			throw new ServiceException(e.getMessage(), e);
		}
	}

	@Override
	public List<BaseDto> findBy(String sql) throws ServiceException {
		try {
			return this.daoFacade.getBaseDao().findBy(sql);
		} catch (DaoLevelException e) {
			throw new ServiceException(e.getMessage(), e);
		}
	}

	@Override
	public BaseDto get(Class clazz, Serializable id) throws ServiceException {
		try {
			return this.daoFacade.getBaseDao().get(clazz, id);
		} catch (DaoLevelException e) {
			throw new ServiceException(e.getMessage(), e);
		}
	}
	
	public DAOFacade getDaoFacade() {
		return daoFacade;
	}

	public void setDaoFacade(DAOFacade daoFacade) {
		this.daoFacade = daoFacade;
	}
	
	public Integer getMaxId(String idName, String className) throws ServiceException {
		try {
			return this.daoFacade.getBaseDao().getMaxId(idName, className);
		} catch (DaoLevelException e) {
			throw new ServiceException(e.getMessage(), e);
		}
	}
}
